﻿using System;
using System.Collections.Generic;
using System.Data;
using NBearLite;
using NBear.Mapping;
using Xin478.CSWork.Common;

namespace Xin478.CSWork.DomainModel
{
    public class EmployeeModel
    {
        public static EmployeeInfo Select(Database db,string username, string password)
        {
            return db.Select(MyWork.Employees)
                .Where(MyWork.Employees.UserName == username && MyWork.Employees.PassWord == System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(password,"MD5"))
                .ToSingleObject<EmployeeInfo>();
        }

        public static EmployeeInfo Select(Database db, int userId)
        {
            return db.Select(MyWork.Employees)
                .Where(MyWork.Employees.Id == userId)
                .ToSingleObject<EmployeeInfo>();
        }

        public static IList<EmployeeInfo> GroupEmployeeList(Database db, int groupId)
        {
            if (groupId == 0)
            {
                return db.Select(MyWork.Employees).OrderBy(MyWork.Employees.Name.Asc).ToList<EmployeeInfo>();
            }
            else
            {
                return db.Select(MyWork.Employees)
                    .RightJoin(MyWork.Employees_Group, MyWork.Employees_Group.EmployeeId == MyWork.Employees.Id)
                    .Where(MyWork.Employees_Group.GroupId == groupId)
                    .ToList<EmployeeInfo>();
            }
        }

    }
}
